package tw.asap.plus.ui.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;

/**
 * Description:
 * Created:      [15/11/2]
 * Author:       [Yang]
 **/
public class CategoryAdapter extends BaseAdapter {

    Context mContext;
    List<PinedSectionAdapter.GoodsItem> mList;

    public CategoryAdapter(Context mContext, List<PinedSectionAdapter.GoodsItem> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mList != null)
            return mList.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_category, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.catName.setText(mList.get(position).text);
        if (mList.get(position).sectionContainCount > 0) {
            if(mList.get(position).sectionContainCount>99){
                viewHolder.tvCounter.setTextSize(TypedValue.COMPLEX_UNIT_SP,8);
            }else{
                viewHolder.tvCounter.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
            }
            viewHolder.tvCounter.setText(String.valueOf(mList.get(position).sectionContainCount));
            viewHolder.tvCounter.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvCounter.setVisibility(View.GONE);
        }
        if (mList.get(position).isSelect) {
            viewHolder.imageView.setVisibility(View.VISIBLE);
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.palette_white));
        } else {
            viewHolder.imageView.setVisibility(View.INVISIBLE);
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.asap_gray));
        }
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_category.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @InjectView(R.id.cat_name)
        TextView catName;
        @InjectView(R.id.tv_counter)
        TextView tvCounter;
        @InjectView(R.id.imageView2)
        ImageView imageView;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
